package com.koron.css2.meterService.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.meterService.bean.MsVerifyBean;
import com.koron.css2.meterService.mapper.MsVerifyMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.ValidationKey;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 校表修改
 * @author bin
 *
 */
public class MsVeriUpdate implements ServerInterface{
	
	private static Logger log = LogManager.getLogger(MsVeriUpdate.class);

	@Override
    @ValidationKey(clazz = MsVerifyBean.class,method = "update")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<Void> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Void.class);
		try {
			MsVerifyBean bean = JsonUtils.objectToPojo(req.getData(), MsVerifyBean.class);
			if(StringUtils.isBlank(bean.getId())) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "id 不能为空", Void.class);
			}
			MsVerifyMapper mapper = factory.getMapper(MsVerifyMapper.class);	
			String billNo = mapper.selectProcessNotEnd(bean.getMeterNo(), bean.getId());
			if(billNo!=null) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "水表编号"+bean.getMeterNo()+"存在未结束的工作流,单据编号:"+billNo, void.class);
			}
			int iCount = mapper.updateMsVerify(bean);
			if(iCount < 1) {
				info.setCode(Constant.MESSAGE_INT_FAIL);
				info.setDescription("修改失败");
			}
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("修改失败");
			log.error(e.getMessage(), e);
		}
		return info;
	}

}
